package cn.parllay.dingdangproject;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.https.HttpsUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.concurrent.TimeUnit;

import cn.parllay.dingdangproject.global.API;
import cn.parllay.dingdangproject.global.DingStoreAPI;
import cn.parllay.dingdangproject.widget.MyWindowManager1;
import okhttp3.OkHttpClient;

/**
 * Created by sunny on 2017/8/17.
 */

public class DingDangApplication extends Application{

    private static final String TAG = "DingDangApplication";
    private static Context context;
    @Override
    public void onCreate() {
        super.onCreate();

        API.setProEnvironment(true);
        DingStoreAPI.setEnviroment(true);

        context = this;
        //初始化OkhttpUtils
        initOkhttpUtils();

        MyWindowManager1.getInstance().init(getApplicationContext());

        initCloudChannel(this);

//        Beta.autoInit = true;
//        Beta.showInterruptedStrategy = true;
//        Beta.initDelay = 3 * 1000;
//        Bugly.init(getApplicationContext(), "9e68874b9c", false);

        //版本升级配置
//        UpdateConfig.init(this);
    }

    /**
     * 初始化云推送通道
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        PushServiceFactory.init(applicationContext);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                Log.d(TAG, "init cloudchannel success");
            }
            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.d(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });

    }

    private void initOkhttpUtils() {

        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .connectTimeout(20000L, TimeUnit.MILLISECONDS)
                .readTimeout(20000L, TimeUnit.MILLISECONDS)
                .addInterceptor(new LoggerInterceptor("parllay"))
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }

    public static Context getContext() {
        return context;
    }


}
